<template>
  <div>
    <el-dialog
      title="自定义事件"
      width="800px"
      :model-value="isDialog"
      :before-close="handleCancel"
    >
      <el-form
        :model="eventData"
        :rules="rules"
        ref="eventData"
        label-width="100px"
      >
        <el-form-item label="事件CODE" prop="propertyKey">
          <el-input
            type="text"
            placeholder="请输入事件CODE"
            @input="setValue('propertyKey')"
            v-model="eventData.propertyKey"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="事件名称" prop="name">
          <el-input
            type="text"
            placeholder="请输入事件名称"
            v-model="eventData.name"
            maxlength="40"
            show-word-limit
          >
          </el-input>
        </el-form-item>


        <el-form-item label="事件分类" prop="typeId">
          <el-select v-model="eventData.typeId" @change="setEventTypeName(eventData.typeId)" placeholder="请选择事件分类">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="类型" prop="propertyType">
          <el-select v-model="eventData.propertyType" @change="setPropertyTypeName(eventData.propertyType)" placeholder="请选择类型">
            <el-option
              v-for="item in propertyTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="设置快捷入口" prop="showInCanvas">
          <el-switch
            v-model="eventData.showInCanvas"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </el-form-item> -->
        <el-form-item label="选择组件icon">
          <el-tag
            v-for="(item, idx) in iconList"
            :key="idx"
            :class="{ 'tag-active': index == idx }"
            @click="setIcon(item, idx)"
          >
            <i :class="item"></i>
          </el-tag>
        </el-form-item>
      </el-form>
      <span  slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import EventApi from "@/api/eventApi";
import { mapMutations } from "vuex";
export default {
  props: {
    data: {
      require: false,
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      isDialog: true,
      typeList: [],
      index: '',
      eventData: {
        name: "",
        typeId: '',
        icon: '',
        event: 'event',
        propertyKey: "",
        showInCanvas: 1,
        propertyType: "",
      },
      propertyTypeList: [
        { id: 0, name: "数字" },
        { id: 1, name: "字符串" },
        { id: 2, name: "时间" },
        { id: 3, name: "日期" },
      ],
      iconList: [
        "el-icon-tickets",
        "el-icon-document-checked",
        "el-icon-attract",
        "el-icon-data-analysis",
      ],
      rules: {
        name: [
          {
            required: true,
            message: "请输入流程名称",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  created() {
    this.eventData = this.data
    this.getSimpleList();
  },
  methods: {
    ...mapMutations("event", ["setPushEventEvent"]),
    formValidate() {
      this.$refs.eventData.validate((valid) => {
        if (valid) {
          if (this.eventData.publishTime && this.eventData.invalidTime) {
            this.active++;
          } else {
            this.$message.warning("请输入时间");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },


  setIcon(item, idx) {
    this.eventData.icon = item
    this.index = idx
  },

  async getSimpleList() {
      let rs = await EventApi.getSimpleList()
      if(rs.code === 200) {
        console.log('[ 事件分类-list ] >', rs.payload)
        this.typeList = rs.payload
      }
    },
  setEventTypeName(id) {
    this.typeList.forEach(item=>{
      if(item.id == id) {
        this.eventData['typeName'] = item.name
      }
    })
  },
  setPropertyTypeName(id) {
    this.propertyTypeList.forEach(item=>{
      if(item.id == id) {
        this.eventData['propertyTypeName'] = item.name
      }
    })
  },
  setValue(propertyKey){
        this.formData[propertyKey] = this.formData[propertyKey].replace(/[^\w_]/g,'')

    },
    handleConfirm() {
      this.$emit("confirm", this.eventData);
      this.setPushEventEvent(this.eventData)
      this.handleCancel()
    },
    handleCancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
:deep(.el-drawer) {
  padding: 0 20px;
  line-height: 1;
}
.el-tag {
  margin-right: 10px;
  cursor: pointer;
}
.tag-active {
  color: red;
}

.form-footer {
  display: flex;
  justify-content: flex-end;
}
.el-input {
  width: 240px;
}
</style>
